
package com.example.attendancesystem.Interceptor;

import com.example.attendancesystem.Models.Admin;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class MyInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        /*if (null == admin){
            request.setAttribute("mess","权限不足");
            return false;
        }
        return true;*/
        String url = request.getRequestURL().toString();
        if (
                null != admin ||
                        url.contains("http://localhost/login") ||
                        url.contains("http://localhost/userLogin") ||
                        url.contains("http://localhost/register") ||
                        url.contains("http://localhost/static/js/jquery-3.1.0.js") ||
                        url.contains("http://localhost/static/layui/css/layui.css") ||
                        url.contains("http://localhost/static/layui/layui.js") ||
                        url.contains("http://localhost/imageVerify") ||
                        url.contains("http://localhost/static/images/login.jpg") ||
                        url.contains("http://localhost/static/images/**") ||
                        url.contains("http://localhost/static/layui/css/modules/laydate/default/laydate.css") ||
                        url.contains("http://localhost/static/layui/css/modules/code.css") ||
                        url.contains("http://localhost/static/layui/css/modules/layer/default/layer.css")) {
            return true;
        }
        /*request.setAttribute("mess", "权限不足");
        request.getRequestDispatcher("/emp/empIndex").forward(request, response);*/
        response.sendRedirect("/emp/empIndex");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) {
    }
}

